package com.jz.cost.service.impl;

import com.jz.cost.enums.CurrencyEnum;
import com.jz.cost.enums.JizhangEnum;
import com.jz.cost.mapper.CostMapper;
import com.jz.cost.model.*;
import com.jz.cost.service.CostService;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.AllRecordsVo;
import com.jz.util.PageVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.jz.common.util.TimeUtil;

import javax.validation.Valid;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class CostServiceImpl implements CostService {

    @Autowired
    private CostMapper costMapper;

    @Override
    public ResultVo firstPage(CostSelectVo costSelectVo) {
        List<CostVo> costVoList = costMapper.queryData(costSelectVo);
        //查询数量
        for (CostVo costVo : costVoList) {

            int status = costVo.getStatus() ? 1 : 0;
            costVo.setStatusName(SubStatusEnum.getNameByStatus(status));
            costVo.setPriceTypeName(JizhangEnum.getNameByStatus(costVo.getPriceType()));
            costVo.setCurrencyCodeName(CurrencyEnum.getNameByStatus(costVo.getCurrencyCode()));

        }
        int count = costMapper.queryCount(costSelectVo);

        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(costVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(costSelectVo.getPageIndex());
        allRecordsVo.setPageSize(costSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addCost(CostAddVo costAddVo) {

        List<CostVo> costVoList = costMapper.queryByPartAndLegal(costAddVo);
        //判断添加对对象是否存在，不存在就直接添加
        if (!CollectionUtils.isEmpty(costVoList)) {
            for (CostVo costVo : costVoList) {
                //1.判断时间是否有交集 自定义一个类TimeUtil
                Boolean aBoolean = TimeUtil.IsInterSection(costAddVo.getStartTime(), costAddVo.getEndTime(), costVo.getStartTime(), costVo.getEndTime());
                if (aBoolean) {
                    //有交集
//                    1.新增开始时间在原数据前
                    if (costAddVo.getStartTime().getTime() <= costVo.getStartTime().getTime()){
                        return ResultBuildVo.error("数据失效");
                    }
//                    1.开始时间一样
                    else  if (costAddVo.getStartTime().getTime() == costVo.getStartTime().getTime()) {
                        System.out.println(2);
                        costMapper.deleteByCostId(costVo.getId());
                        Date nowDate = new Date();
                        CostVo costVo1 = new CostVo();
                        BeanUtils.copyProperties(costAddVo, costVo1);
                        costVo1.setCreateTime(nowDate);
                        costVo1.setCreateName("通过Session拿到当前用户名称");
                        costMapper.add(costVo1);
                        return ResultBuildVo.success();
                    } else {
                        //新增结束时间在旧时间前
                        if (costAddVo.getEndTime().getTime() <= costVo.getEndTime().getTime()) {
                            //1.添加新增的
                            System.out.println(4);
                            Date nowDate = new Date();
                            CostVo costVo1 = new CostVo();
                            BeanUtils.copyProperties(costAddVo, costVo1);
                            costVo1.setCreateTime(nowDate);
                            costVo1.setCreateName("通过Session拿到当前用户名称");
                            costMapper.add(costVo1);
                            //修改结束日期减1天
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(costAddVo.getStartTime());
                            calendar.add(Calendar.DATE, -1);
                            Date date1 = calendar.getTime();
                            CostVo costVo2 = new CostVo();
                            BeanUtils.copyProperties(costVo, costVo2);
                            costVo2.setEndTime(date1);
                            costVo2.setUpdateTime(nowDate);
                            costVo2.setUpdateName("通过Session拿到当前用户名称");
                            costMapper.updateEndTime(costVo2);
                            //添加开始时间加1
                            Calendar calendar1 = Calendar.getInstance();
                            calendar1.setTime(costAddVo.getEndTime());
                            calendar1.add(Calendar.DATE, 1);
                            Date date2 = calendar1.getTime();
                            CostVo costVo3 = new CostVo();
                            BeanUtils.copyProperties(costVo, costVo3);
                            costVo3.setStartTime(date2);
                            costVo3.setCreateTime(nowDate);
                            costVo3.setCreateName("通过Session拿到当前用户名称");
                            costMapper.addP(costVo3);
                            return ResultBuildVo.success();
                        } else {
                            System.out.println(5);
                            //新增结束时间在旧时间后
                            Date nowDate = new Date();
                            CostVo costVo1 = new CostVo();
                            BeanUtils.copyProperties(costAddVo, costVo1);
                            costVo1.setCreateTime(nowDate);
                            costVo1.setCreateName("通过Session拿到当前用户名称");
                            costMapper.add(costVo1);
                            System.out.println(000);
                            //修改结束日期减1天
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(costAddVo.getStartTime());
                            calendar.add(Calendar.DATE, -1);
                            Date date1 = calendar.getTime();
                            CostVo costVo2 = new CostVo();
                            BeanUtils.copyProperties(costVo, costVo2);
                            costVo2.setEndTime(date1);
                            costVo2.setUpdateTime(nowDate);
                            costVo2.setUpdateName("通过Session拿到当前用户名称");
                            costMapper.updateEndTime(costVo2);
                            return ResultBuildVo.success();
                        }
                    }

                } else {
                    //没有交集
                    System.out.println(6);
                    Date nowDate = new Date();
                    CostVo costVo1 = new CostVo();
                    BeanUtils.copyProperties(costAddVo, costVo1);
                    costVo1.setCreateTime(nowDate);
                    costVo1.setCreateName("通过Session拿到当前用户名称");
                    costMapper.add(costVo1);
                    return ResultBuildVo.success();
                }
            }
    }
        System.out.println(7);
        Date nowDate = new Date();
        CostVo costVo = new CostVo();
        BeanUtils.copyProperties(costAddVo, costVo);
        costVo.setCreateTime(nowDate);
        costVo.setCreateName("通过Session拿到当前用户名称");
        costMapper.add(costVo);
        return ResultBuildVo.success();
    }


    //    @Override
//    public ResultVo queryById(Integer id) {
//
//        CatVo partVo = costMapper.queryById(id);
//
//        return ResultBuildVo.success(partVo);
//    }
////

    @Override
    public ResultVo queryByUserId(Integer userId) {

        List<CostVo> legalPersonLists = costMapper.queryByUserId(userId);

        return ResultBuildVo.success(legalPersonLists);
    }

    @Override
    public ResultVo queryPrice() {

        List<PriceVo> priceVoList = costMapper.queryPrice();
        return ResultBuildVo.success(priceVoList);
    }

    @Override
    public ResultVo queryTax() {
        List<TaxVo> taxVoList = costMapper.queryTax();

        return ResultBuildVo.success(taxVoList);

    }

    //    public ResultVo queryAll() {
//
//       List<SubClassVo> subClassVoList =  costMapper.queryAll();
//
//       return ResultBuildVo.success(subClassVoList);
//    }
//
//    @Override
//    public ResultVo queryAll2() {
//
//      List<PlanClassVo> planClassVoList =   costMapper.queryAll2();
//        return ResultBuildVo.success(planClassVoList);
//    }
//
////
    @Override
    public ResultVo updateStatus(@Valid CostUpdateStatusVo costUpdateStatusVo) {
        List<CostVo> costVo = costMapper.queryById(costUpdateStatusVo.getId());
        if (costVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        CostVo classVo = new CostVo();
        BeanUtils.copyProperties(costUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        costMapper.updateStatus(classVo);

        return ResultBuildVo.success();

    }
    //3.修改
    @Override
    public ResultVo updateCost(@Valid CostUpdateVo costUpdateVo) {

        List<CostVo> costVoList = costMapper.queryByPartAndLegal2(costUpdateVo);
        //判断添加对对象是否存在，不存在就直接添加
        if (costVoList.size() > 1) {
            for (CostVo costVo : costVoList) {
                //1.判断时间是否有交集 自定义一个类TimeUtil
                Boolean aBoolean = TimeUtil.IsInterSection(costUpdateVo.getStartTime(), costUpdateVo.getEndTime(), costVo.getStartTime(), costVo.getEndTime());
                if (aBoolean) {
                    //有交集
//                    1.新增开始时间在原数据前
                    if (costUpdateVo.getStartTime().getTime() <= costVo.getStartTime().getTime()){
                        return ResultBuildVo.error("数据失效");
                    }
//                    1.开始时间一样
                    else  if (costUpdateVo.getStartTime().getTime() == costVo.getStartTime().getTime()) {
                        costMapper.deleteByCostId(costVo.getId());
                        Date nowDate = new Date();
                        CostVo costVo1 = new CostVo();
                        BeanUtils.copyProperties(costUpdateVo, costVo1);
                        costVo1.setUpdateTime(nowDate);
                        costVo1.setUpdateName("通过Session拿到当前用户名称");
                        costMapper.updateCost(costVo1);
                        return ResultBuildVo.success();
                    } else {
                        //新增结束时间在旧时间前
                        if (costUpdateVo.getEndTime().getTime() <= costVo.getEndTime().getTime()) {
                            //1.修改新增的
                            Date nowDate = new Date();
                            CostVo costVo1 = new CostVo();
                            BeanUtils.copyProperties(costUpdateVo, costVo1);
                            costVo1.setUpdateTime(nowDate);
                            costVo1.setUpdateName("通过Session拿到当前用户名称");
                            costMapper.updateCost(costVo1);
                            //修改结束日期减1天
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(costUpdateVo.getStartTime());
                            calendar.add(Calendar.DATE, -1);
                            Date date1 = calendar.getTime();
                            CostVo costVo2 = new CostVo();
                            BeanUtils.copyProperties(costVo, costVo2);
                            costVo2.setEndTime(date1);
                            costVo2.setUpdateTime(nowDate);
                            costVo2.setUpdateName("通过Session拿到当前用户名称");
                            costMapper.updateEndTime(costVo2);
                            //添加开始时间加1
                            Calendar calendar1 = Calendar.getInstance();
                            calendar1.setTime(costUpdateVo.getEndTime());
                            calendar1.add(Calendar.DATE, 1);
                            Date date2 = calendar1.getTime();
                            CostVo costVo3 = new CostVo();
                            BeanUtils.copyProperties(costVo, costVo3);
                            costVo3.setStartTime(date2);
                            costVo3.setCreateTime(nowDate);
                            costVo3.setCreateName("通过Session拿到当前用户名称");
                            costMapper.addP(costVo3);
                            return ResultBuildVo.success();
                        } else {
                            //新增结束时间在旧时间后
                            Date nowDate = new Date();
                            CostVo costVo1 = new CostVo();
                            BeanUtils.copyProperties(costUpdateVo, costVo1);
                            costVo1.setUpdateTime(nowDate);
                            costVo1.setUpdateName("通过Session拿到当前用户名称");
                            costMapper.updateCost(costVo1);
                            //修改结束日期减1天
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(costUpdateVo.getStartTime());
                            calendar.add(Calendar.DATE, -1);
                            Date date1 = calendar.getTime();
                            CostVo costVo2 = new CostVo();
                            BeanUtils.copyProperties(costVo, costVo2);
                            costVo2.setEndTime(date1);
                            costVo2.setUpdateTime(nowDate);
                            costVo2.setUpdateName("通过Session拿到当前用户名称");
                            costMapper.updateEndTime(costVo2);
                            return ResultBuildVo.success();
                        }
                    }

                } else {
                    //没有交集
                    Date nowDate = new Date();
                    CostVo costVo1 = new CostVo();
                    BeanUtils.copyProperties(costUpdateVo, costVo1);
                    costVo1.setUpdateTime(nowDate);
                    costVo1.setUpdateName("通过Session拿到当前用户名称");
                    costMapper.updateCost(costVo1);
                    return ResultBuildVo.success();
                }
            }
        }
        Date nowDate = new Date();
        CostVo costVo = new CostVo();
        BeanUtils.copyProperties(costUpdateVo, costVo);
        costVo.setUpdateTime(nowDate);
        costVo.setUpdateName("通过Session拿到当前用户名称");
        costMapper.updateCost(costVo);
        return ResultBuildVo.success();
    }






}

